﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Web;
using Sealight.API.Framework;
using System.Drawing.Drawing2D;
using ImageDrawing = System.Drawing.Image;

namespace Sealight.UI.WebUI.UIHelpers
{
 
        public class ImageObject
        {
            public static string SaveImage(HttpContext context, Guid guid)
            {
                if (context.Request.Files.Count > 0)
                {
                    string strFileName = Path.GetFileName(context.Request.Files[0].FileName);
                    string strExtension = Path.GetExtension(context.Request.Files[0].FileName).ToLower();
                    string strImagesLocation = context.Server.MapPath(GlobalConstant.IMAGES_FOLDER).TrimEnd('\\');
                    string strUsersLocation = Path.Combine(strImagesLocation, "common");

                    if (!Directory.Exists(strUsersLocation))
                    {
                        Directory.CreateDirectory(strUsersLocation);
                    }

                    if (!string.IsNullOrEmpty(strExtension))
                    {
                        string fileRaw = string.Format("{0}{1}", guid.ToString(), strExtension);

                        context.Request.Files[0].SaveAs(string.Format("{0}\\{1}", strUsersLocation, fileRaw));

                        ImageFormat format = ImageFormat.Png;

                        if (strExtension.ToLower() == "gif")
                        {
                            format = ImageFormat.Gif;
                        }
                        else if (strExtension.ToLower() == "png")
                        {
                            format = ImageFormat.Png;
                        }                        
                        return fileRaw;
                    }
                }

                return string.Empty;

            }

            public static string SaveImages(HttpContext context, Guid guid)
            {
                int n = context.Request.Files.Count;
                if (n > 0)
                {
                    string urlStream = "";
                    for (int i = 0; i < n; i++)
                    {
                        string strFileName = Path.GetFileName(context.Request.Files[i].FileName);
                        string strExtension = Path.GetExtension(context.Request.Files[i].FileName).ToLower();
                        string strImagesLocation = context.Server.MapPath(GlobalConstant.IMAGES_FOLDER).TrimEnd('\\');
                        string strUsersLocation = Path.Combine(strImagesLocation, "common");

                        if (!Directory.Exists(strUsersLocation))
                        {
                            Directory.CreateDirectory(strUsersLocation);
                        }

                        if (!string.IsNullOrEmpty(strExtension))
                        {
                            string fileRaw = string.Format("{0}{1}", guid.ToString(), strExtension);

                            context.Request.Files[i].SaveAs(string.Format("{0}\\{1}", strUsersLocation, fileRaw));

                            ImageFormat format = ImageFormat.Png;

                            if (strExtension.ToLower() == "gif")
                            {
                                format = ImageFormat.Gif;
                            }
                            else if (strExtension.ToLower() == "png")
                            {
                                format = ImageFormat.Png;
                            }

                            if (i == 0)
                            {
                                urlStream = fileRaw;
                            }
                            else
                            {
                                urlStream += "|" + fileRaw;
                            }
                        }
                    }
                    return urlStream;
                }

                return string.Empty;
            }

            public static string SaveImages(HttpContext context, Guid guid, string folder)
            {
                int n = context.Request.Files.Count;
                if (n > 0)
                {
                    string urlStream = "";
                    for (int i = 0; i < n; i++)
                    {
                        string strFileName = Path.GetFileName(context.Request.Files[i].FileName);
                        string strExtension = Path.GetExtension(context.Request.Files[i].FileName).ToLower();
                        string strImagesLocation = context.Server.MapPath(GlobalConstant.IMAGES_FOLDER).TrimEnd('\\');
                        string strUsersLocation = Path.Combine(strImagesLocation, folder);

                        if (!Directory.Exists(strUsersLocation))
                        {
                            Directory.CreateDirectory(strUsersLocation);
                        }

                        if (!string.IsNullOrEmpty(strExtension))
                        {
                            string fileRaw = string.Format("{0}{1}", guid.ToString(), strExtension);

                            context.Request.Files[i].SaveAs(string.Format("{0}\\{1}", strUsersLocation, fileRaw));

                            ImageFormat format = ImageFormat.Png;

                            if (strExtension.ToLower() == "gif")
                            {
                                format = ImageFormat.Gif;
                            }
                            else if (strExtension.ToLower() == "png")
                            {
                                format = ImageFormat.Png;
                            }

                            if (i == 0)
                            {
                                urlStream = fileRaw;
                            }
                            else
                            {
                                urlStream += "|" + fileRaw;
                            }
                        }
                    }
                    return urlStream;
                }

                return string.Empty;

            }

            public static string SaveImage(HttpContext context, Guid guid, string folder)
            {
                if (context.Request.Files.Count > 0)
                {
                    string strFileName = Path.GetFileName(context.Request.Files[0].FileName);
                    string strExtension = Path.GetExtension(context.Request.Files[0].FileName).ToLower();
                    string strImagesLocation = context.Server.MapPath(GlobalConstant.IMAGES_FOLDER).TrimEnd('\\');
                    string strUsersLocation = Path.Combine(strImagesLocation, folder);

                    if (!Directory.Exists(strUsersLocation))
                    {
                        Directory.CreateDirectory(strUsersLocation);
                    }

                    if (!string.IsNullOrEmpty(strExtension))
                    {
                        string fileRaw = string.Format("{0}{1}", guid.ToString(), strExtension);

                        context.Request.Files[0].SaveAs(string.Format("{0}\\{1}", strUsersLocation, fileRaw));

                        ImageFormat format = ImageFormat.Png;

                        if (strExtension.ToLower() == "gif")
                        {
                            format = ImageFormat.Gif;
                        }
                        else if (strExtension.ToLower() == "png")
                        {
                            format = ImageFormat.Png;
                        }
                        
                        return fileRaw;
                    }
                }

                return string.Empty;

            }

            public static string SaveImage(HttpContext context, HttpPostedFileBase upload, Guid guid, string folder)
            {
                if (upload!= null)
                {
                    string strFileName = Path.GetFileName(upload.FileName);
                    string strExtension = Path.GetExtension(upload.FileName).ToLower();
                    string strImagesLocation = context.Server.MapPath(GlobalConstant.IMAGES_FOLDER).TrimEnd('\\');
                    string strUsersLocation = Path.Combine(strImagesLocation, folder);

                    if (!Directory.Exists(strUsersLocation))
                    {
                        Directory.CreateDirectory(strUsersLocation);
                    }

                    if (!string.IsNullOrEmpty(strExtension))
                    {
                        string fileRaw = string.Format("{0}{1}", guid.ToString(), strExtension);

                        context.Request.Files[0].SaveAs(string.Format("{0}\\{1}", strUsersLocation, fileRaw));

                        ImageFormat format = ImageFormat.Png;

                        if (strExtension.ToLower() == "gif")
                        {
                            format = ImageFormat.Gif;
                        }
                        else if (strExtension.ToLower() == "png")
                        {
                            format = ImageFormat.Png;
                        }

                        return fileRaw;
                    }
                }

                return string.Empty;

            }

            private static void SaveImageHxW(string imgPath, string fileName, int w, int h, ImageFormat format)
            {
                string newFileImage = string.Empty;

                System.Drawing.Image imageObject = System.Drawing.Image.FromFile(Path.Combine(imgPath, fileName));

                Bitmap bm = null;

                bm = new Bitmap(imageObject, w, h);

                newFileImage = fileName.Insert(0, string.Format("{0}x{1}_", w, h));
                bm.Save(Path.Combine(imgPath, newFileImage), format);

                bm.Dispose();
                imageObject.Dispose();
            }

            public static bool CheckExt(string ext)
            {
                bool fileOK = false;
                string[] allowedExtensions = { ".png", ".gif", ".jpg", ".jpeg" };

                for (int i = 0; i < allowedExtensions.Length; i++)
                    if (ext == allowedExtensions[i])
                        fileOK = true;

                return fileOK;
            }

            public static ImageDrawing Scale(ImageDrawing imgPhoto, int Width, int Height, string bg)
            {
                int sourceWidth = imgPhoto.Width;
                int sourceHeight = imgPhoto.Height;
                int sourceX = 0;
                int sourceY = 0;
                int destX = 0;
                int destY = 0;

                float nPercent = 0;
                float nPercentW = 0;
                float nPercentH = 0;

                nPercentW = ((float)Width / (float)sourceWidth);
                nPercentH = ((float)Height / (float)sourceHeight);
                if (nPercentH < nPercentW)
                {
                    nPercent = nPercentH;
                    destX = Convert.ToInt16((Width - (sourceWidth * nPercent)) / 2);
                }
                else
                {
                    nPercent = nPercentW;
                    destY = System.Convert.ToInt16((Height - (sourceHeight * nPercent)) / 2);
                }
                int destWidth = (int)(sourceWidth * nPercent);
                int destHeight = (int)(sourceHeight * nPercent);

                Bitmap bmPhoto = new Bitmap(Width, Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
                bmPhoto.SetResolution(imgPhoto.HorizontalResolution, imgPhoto.VerticalResolution);

                Graphics grPhoto = Graphics.FromImage(bmPhoto);
                Color c = System.Drawing.ColorTranslator.FromHtml(bg);

                grPhoto.Clear(c);
                grPhoto.InterpolationMode = InterpolationMode.HighQualityBicubic;
                grPhoto.DrawImage(imgPhoto, new Rectangle(destX, destY, destWidth, destHeight),
                  new Rectangle(sourceX, sourceY, sourceWidth, sourceHeight),
                GraphicsUnit.Pixel);

                grPhoto.Dispose();
                return bmPhoto;
            }
        }
 
}